package com.week.tiny_video.controller;

import com.week.tiny_video.pojo.Comment;
import com.week.tiny_video.pojo.User;
import com.week.tiny_video.result.Result;
import com.week.tiny_video.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@CrossOrigin(allowCredentials = "true")
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    //发表评论
    @PostMapping("comment")
    public Result add(@RequestBody Comment comment, HttpSession session){
        comment.setTime(new Date());
        User user = (User) session.getAttribute("user");
        comment.setUser(user);
        commentService.add(comment);
        return new Result(200,"评论成功",true);

    }

    //显示某个视频下的评论列表
    @GetMapping("comment/video/{id}")
    public Result listByVideo(@PathVariable("id") Integer vid){
        List<Comment> comments = commentService.listByVideo(vid);
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",comments);
        return new Result(200,"单个视频下的评论",true,map);

    }

    @DeleteMapping("comment/{id}")
    public Result delete(@PathVariable("id") Integer id){
        commentService.delete(id);
        return new Result(200,"删除成功",true);

    }
}
